package mi.payment.ui.print.helper;

import android.graphics.Bitmap;
import android.text.TextUtils;

import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.SalesSlipPrintSettings;
import mi.payment.settings.SalesSlipPrintSettingsOperation;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.SystemDateTime;
import sunmi.payment.L3.L3PrintUtil;
import woyou.aidlservice.jiuiv5.ICallback;

/**
 * 扫码类小票打印，微信、支付宝
 */
public class PrintCodeTicket extends BasePrint {

    private PayDetail payDetail;
    private boolean isPrintEnglish;

    public PrintCodeTicket() {
        super();
    }

    public void printCodeTicket(PayDetail payDetail, boolean isAgain, int pageNum, boolean isBarCode, ICallback callback) throws Exception {
        this.payDetail = payDetail;
        SalesSlipPrintSettings salesSlipPrintSettings = SalesSlipPrintSettingsOperation.getInstance().getSalesSlipPrintSettings();
        isPrintEnglish = salesSlipPrintSettings.isSalesSlipPrintEnglish(); // 是否打印英文
        isPrintEnglish = false;

        int verySmallFontSize = 14;
        int litterSmallFontSize = 16;
        int smallFontSize = 18;
        int normalFontSize = 20;
        if (salesSlipPrintSettings.getSelectSalesSlipFont() > 0) {
            // 大号字体
            verySmallFontSize += 2;
            litterSmallFontSize += 2;
            smallFontSize += 2;
            normalFontSize += 2;
        }

        printerService.enterPrinterBuffer(true);
        printLine();

        // 打印logo
        printTitleLogo(callback);

        // 打印存根信息
        printMerchantOrUser(pageNum, isPrintEnglish, verySmallFontSize);

        // 打印商户信息
        printMerchantInfo(normalFontSize);

        // 打印订单描述
        printSeparateLine();
        printOrderDescribes(normalFontSize);
        printSeparateLine();

        setBold(true);
        printerService.setAlignment(1, null);
        String remark;
        String platform = PrintUtil.getTransactionPlatform(payDetail.transPlatform + "");
        if (payDetail.qrCodeScanModel == 1) {
            remark = getString(R.string.receipt_merchant_scan) + " - " + platform + "\n";
        } else {
            remark = getString(R.string.receipt_user_scan) + " - " + platform + "\n";
        }
        printerService.printTextWithFont(remark, null, 24, null);
        printerService.setAlignment(0, null);
        setBold(false);
        printSeparateLine();

        // 打印二维码
        printCode(callback);

        // 打印本人确认以上交易同意记入本卡帐户
        String temp = getString(R.string.receipt_agree_trade) + "\n";
        printerService.printTextWithFont(temp, null, litterSmallFontSize, null);
        printLine();

        // 重打印
        printAgainText(isAgain, isPrintEnglish, smallFontSize);

        // 打印小票底部信息
        printFooter(callback, smallFontSize);

        // L3用户追加打印
        if (pageNum == 1) {
            L3PrintUtil.l3MerchantAddPrinter(printerService);
        } else if (pageNum == 2) {
            L3PrintUtil.l3UserAddPrinter(printerService);
        }

        printerService.lineWrap(4, callback);
        printerService.exitPrinterBufferWithCallback(true, callback);
    }

    // 打印logo
    private void printTitleLogo(final ICallback callback) throws Exception {
        printerService.setAlignment(1, callback);
        int rise = SalesSlipPrintSettingsOperation.getInstance().getSalesSlipPrintSettings().getRise();
        if (rise == 0) {
            Bitmap bitmap = getPrintTitleIcon(-1);
            printerService.printBitmap(bitmap, callback);
        } else if (rise == 1) {
            Bitmap bitmap = getPrintTitleIcon(0);
            printerService.printBitmap(bitmap, callback);
        } else if (rise == 2) {
            // Bitmap bitmap = getPrintTitleIcon(-2);
            Bitmap bitmap = getPrintTitleIcon(0);
            printerService.printBitmap(bitmap, callback);
        } else {
            Bitmap bitmap = getPrintTitleIcon(0);
            printerService.printBitmap(bitmap, callback);
        }
        printLine();
    }

    /**
     * 打印订单描述
     */
    private void printOrderDescribes(int size) throws Exception {
        String paymentType;
        if (payDetail.transactionType == 2 || payDetail.transactionType == 3) {
            paymentType = getString(R.string.consume_revoke);
        } else {
            paymentType = getString(R.string.consume);
        }
        // 交易类型
        setBold(true);
        String temp = getString(R.string.receipt_type) + paymentType + "\n";
        printerService.printTextWithFont(temp, null, 28, null);
        setBold(false);

        // 凭证号
        temp = getString(R.string.receipt_voucher_number) + payDetail.voucherNo + "\n";
        printerService.printTextWithFont(temp, null, size, null);

        // 参考号
        temp = getString(R.string.receipt_reference_number) + payDetail.referNo + "\n";
        printerService.printTextWithFont(temp, null, size, null);

        // 订单号
        temp = getString(R.string.receipt_order_number) + payDetail.thirdTransNum + "\n";
        printerService.printTextWithFont(temp, null, size, null);

        // 日期
        String printerData = getString(R.string.receipt_date) + SystemDateTime.getTimeByPayDetails(payDetail) + "\n";
        printerService.printTextWithFont(printerData, null, size, null);

        setBold(true);
        String amount = MoneyUtils.longCent2DoubleMoneyStr(payDetail.amount);
        if (payDetail.getTransactionType() == 2 || payDetail.getTransactionType() == 3) {
            temp = getString(R.string.receipt_revoke_amount) + "RM " + amount + "\n";
            printerService.printTextWithFont(temp, null, 28, null);
        } else {
            temp = getString(R.string.receipt_payment_amount) + "RM " + amount + "\n";
            printerService.printTextWithFont(temp, null, 28, null);
        }
        setBold(false);
    }

    /**
     * 打印二维码
     */
    private void printCode(final ICallback callback) throws Exception {
        if (payDetail.thirdTransNum == null || payDetail.thirdTransNum.length() == 0) return;
        // 打印二维码
        printLine();
        printerService.setAlignment(1, null);
        printerService.printQRCode(payDetail.thirdTransNum, 8, 2, callback);
        printerService.setAlignment(0, null);
        printLine();

        // 打印一维码
        // printLine();
        // printerService.setAlignment(1, null);
        // printerService.printBarCode(payDetail.thirdTransNum, 8, 100, 2, 2, null);
        // printerService.setAlignment(0, null);
        // printLine();
    }

    /**
     * 打印商户信息
     */
    private void printMerchantInfo(int size) throws Exception {
        // 打印商户名称
        String merchantName = TextUtils.isEmpty(payDetail.tempMerchantName) ? payDetail.merchantName : payDetail.tempMerchantName;
        merchantName = TextUtils.isEmpty(merchantName) ? "--" : merchantName;
        String temp = getString(R.string.receipt_merchant_name) + merchantName + "\n";
        printerService.printTextWithFont(temp, null, size, null);

        // 打印商户号
        String merchantNo = TextUtils.isEmpty(payDetail.tempMerchantNo) ? payDetail.merchantNo : payDetail.tempMerchantNo;
        merchantNo = TextUtils.isEmpty(merchantNo) ? "--" : merchantNo;
        temp = getString(R.string.receipt_merchant_number) + merchantNo + "\n";
        printerService.printTextWithFont(temp, null, size, null);

        // 打印终端号
        String terminalNo = TextUtils.isEmpty(payDetail.tempTerminalNo) ? payDetail.terminalNo : payDetail.tempTerminalNo;
        terminalNo = TextUtils.isEmpty(terminalNo) ? "--" : terminalNo;
        temp = getString(R.string.receipt_terminal_number) + terminalNo + "\n";
        printerService.printTextWithFont(temp, null, size, null);

        // 操作员号
        temp = getString(R.string.receipt_operator_number) + payDetail.operatorNo + "\n";
        printerService.printTextWithFont(temp, null, size, null);
    }


}
